<?php

class CalendarController extends Zend_Controller_Action
{

    private  $auth;
    
    public function init()
    {
        $this->auth=  Zend_Auth::getInstance();
        if(!$this->auth->hasIdentity()){
             $this->_redirect('index');
      }
    }

    public function indexAction()
    {
       if($_SESSION['role']=='R'){
            $this->_redirect("calendar/view");
       }elseif($_SESSION['role']=='A'){
             $this->_redirect("calendar/edit");
       }
    }

    public function viewAction()
    {
        $this->view->title="Calendar & Events - Welcome to Rosemoore Lake";
    }

    public function editAction()
    {
        $this->view->title="Calendar & Events - Welcome to Rosemoore Lake";
        if($_SESSION['role']!='A'){
             $this->_redirect("calendar/view");
       }
    }

     public function addAction()
    {
        try{
            if(isset ($_POST["description"])){
               $description= $_POST["description"];
               $startDate=$_POST["startDate"];
               $startTime=$_POST["startTime"];
               $endDate=$_POST["endDate"];
               $endTime=$_POST["endTime"];
               $background=$_POST["background"];
               $foreground=$_POST["foreground"];

               $date = new Zend_Date();

               $calendar = new Application_Model_Calendar();
               $calendar->setDescription($description)
                       ->setStartDate(date('Y/m/d',strtotime($startDate)))
                       ->setStartTime($startTime)
                       ->setEndDate(date('Y/m/d',strtotime($endDate)))
                       ->setEndTime($endTime)
                       ->setForeground($foreground)
                       ->setBackground($background)
                       ->setCreatedBy($this->auth->getStorage()->read()->EMAIL_ADDRESS)
                       ->setCreatedDate($date->getIso());

                 $calendarMapper = new Application_Model_CalendarMapper();

                $eventId = $calendarMapper->insert($calendar);

                 echo  $eventId;
            
            }
        }catch(Exception $e){
            Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
            echo "N";
        }
        exit();
    }

     public function updateAction()
    {
        try{
            if(isset ($_POST["description"])){
               $eventId= $_POST["eventId"];
               $description= $_POST["description"];
               $startDate=$_POST["startDate"];
               $startTime=$_POST["startTime"];
               $endDate=$_POST["endDate"];
               $endTime=$_POST["endTime"];
               $background=$_POST["background"];
               $foreground=$_POST["foreground"];

               $date = new Zend_Date();

               $calendar = new Application_Model_Calendar();
               $calendar->setEventId($eventId)
                       ->setDescription($description)
                       ->setStartDate(date('Y-m-d',strtotime($startDate)))
                       ->setStartTime($startTime)
                       ->setEndDate(date('Y-m-d',strtotime($endDate)))
                       ->setEndTime($endTime)
                       ->setForeground($foreground)
                       ->setBackground($background)
                       ->setUpdatedBy($this->auth->getStorage()->read()->EMAIL_ADDRESS)
                       ->setUpdatedDate($date->getIso());

                 $calendarMapper = new Application_Model_CalendarMapper();

                $calendarMapper->update($calendar);

                echo  "Y";

            }
        }catch(Exception $e){
            Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
            echo "N";
            }
        exit();
    }
      public function getAction()
    {
       $calendarMapper = new Application_Model_CalendarMapper();
       $rows = $calendarMapper->get($_POST["startDate"]);
       $entries   = array();
       foreach ($rows as $data) {
           $calendar = new Application_Model_Calendar();
              $calendar->setEventId($data["EVENT_ID"])
                      ->setDescription($data["DESCRIPTION"])
                      ->setStartDate($data["START_DATE"])
                      ->setStartTime($data["START_TIME"])
                      ->setEndDate($data["END_DATE"])
                      ->setEndTime($data["END_TIME"])
                      ->setCreatedBy($data["CREATED_BY"])
                      ->setCreatedDate($data["CREATED_DATE"])
                      ->setForeground($data["FOREGROUND"])
                      ->setBackground($data["BACKGROUND"]);
                       $entries[] = $calendar;
        }
        echo json_encode($entries);
         exit();
    }

      public function deleteAction()
    {
          try{
                    $calendarMapper = new Application_Model_CalendarMapper();
                    $calendarMapper->deleteAgenda($_POST["eventId"]);

                    echo "Y";
          }catch(Exception $e){
              Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
                echo "N";
          }
        exit();
    }
}

